package com.huanglj.himalaya.presenters;

import android.content.Context;

import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.interfaces.IAlbumDetailViewCallback;
import com.huanglj.himalaya.interfaces.IAlbumDetailPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.UILoader;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlbumDetailPresenter implements IAlbumDetailPresenter {

    private static final String TAG = "AlbumDetailPresenter";


    private List<IAlbumDetailViewCallback> mCallBacks = new ArrayList<>();
    private Album mAlbum;
    private int mAlbumId;
    private int mPage;
    private List<Track> mTracks = new ArrayList<>();

    private AlbumDetailPresenter(){

    }

    private static AlbumDetailPresenter sInstance = null;

    public static AlbumDetailPresenter getInstance(){
        if(sInstance == null){
            synchronized (AlbumDetailPresenter.class){
                if(sInstance == null){
                    sInstance = new AlbumDetailPresenter();
                }
            }
        }
        return sInstance;
    }
    @Override
    public void pullRefreshMore() {

    }

    @Override
    public void loadMore() {
        mPage ++;
        onLoadList(true);
    }


    private void onLoadList(final boolean isLoadMore){
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.ALBUM_ID, String.valueOf(mAlbumId));
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE,mPage+"");
        CommonRequest.getInstanse().setDefaultPagesize(20);
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                List<Track> tracks = trackList.getTracks();
                LogUtil.d(TAG, "tracks.size: " + tracks.size());

                if (isLoadMore == true) {
                        mTracks.addAll(tracks);
                        handleAlbumSize(tracks.size());
                } else {
                    mTracks.addAll(0, tracks);
                }
                handleAlbumResult(mTracks);
                UILoader.mRefreshLayout.setRefreshing(false);


            }

            @Override
            public void onError(int i, String s) {
                if (mCallBacks != null) {
                    if (isLoadMore) {
                        mPage --;
                    }
                    for (IAlbumDetailViewCallback callBack : mCallBacks) {
                        callBack.onNetworkError();
                    }
                }
            }
        });
    }

    private void handleAlbumSize(int size) {
        for (IAlbumDetailViewCallback callBack : mCallBacks) {
            callBack.loadFinish(size);
        }
    }

    public void getAlbumDetail(int albumId, int page) {
        mTracks.clear();
        mAlbumId = albumId;
        mPage = page;
        loading();
       onLoadList(false);

    }
    private void loading(){
        if (mCallBacks != null) {
            for (IAlbumDetailViewCallback callBack : mCallBacks) {
                callBack.onLoading();
            }
        }
    }
    /**
     * 处理数据进ui
     * @param track
     */
    private void handleAlbumResult(List<Track> track) {
        if (mCallBacks != null) {
            for (IAlbumDetailViewCallback callBack : mCallBacks) {
                if(track.size()!=0){
                    callBack.onDetailListLoaded(track);
                }else {
                    callBack.onEmpty();
                }
            }
        }
    }

    @Override
    public void registerViewCallBack(IAlbumDetailViewCallback callback) {
        if(mCallBacks != null && !mCallBacks.contains(callback)){
            mCallBacks.add(callback);
            callback.onAlbumLoader(mAlbum);
        }
    }

    @Override
    public void unRegisterViewCallBack(IAlbumDetailViewCallback callBack) {
        if (mCallBacks!=null){
            mCallBacks.remove(callBack);
        }
    }

    public void setTargetAlbum(Album album){
        mAlbum = album;
    }
}
